<!DOCTYPE html>
<html lang="en">
<head><meta name="generator" content="Hexo 3.9.0">
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="google-site-verification" content="xBT4GhYoi5qRD5tr338pgPM5OWHHIDR6mNg1a3euekI">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta name="description" content="David的博客 |  David&#39;s Blog">
    <meta name="keyword" content="程序员小冰 David&#39;s Blog">
    <link rel="shortcut icon" href="/img/favicon.ico">

    <title>
        
        【Shell脚本】贪吃蛇小游戏 - David 的博客 | David&#39;s Blog
        
    </title>

    <!-- Custom CSS -->
    <link rel="stylesheet" href="/css/aircloud.css">
    <link rel="stylesheet" href="/css/gitment.css">
    <!--<link rel="stylesheet" href="https://imsun.github.io/gitment/style/default.css">-->
    <link href="//at.alicdn.com/t/font_620856_pl6z7sid89qkt9.css" rel="stylesheet" type="text/css">
    <!-- ga & ba script hoook -->
    <script></script>
</head>

<body>

<div class="site-nav-toggle" id="site-nav-toggle">
    <button>
        <span class="btn-bar"></span>
        <span class="btn-bar"></span>
        <span class="btn-bar"></span>
    </button>
</div>

<div class="index-about">
    <i> David Blog </i>
</div>

<div class="index-container">
    
    <div class="index-left">
        
<div class="nav" id="nav">
    <div class="avatar-name">
        <div class="avatar radius">
            <img src="/img/avatar.jpg" />
        </div>
        <div class="name">
            <i>David</i>
        </div>
    </div>
    <div class="contents" id="nav-content">
        <ul>
            <li >
                <a href="/">
                    <i class="iconfont icon-shouye1"></i>
                    <span>主页</span>
                </a>
            </li>
            <li >
                <a href="/tags">
                    <i class="iconfont icon-biaoqian1"></i>
                    <span>标签</span>
                </a>
            </li>
            <li >
                <a href="/archives">
                    <i class="iconfont icon-guidang2"></i>
                    <span>存档</span>
                </a>
            </li>
            <li >
                <a href="/about/">
                    <i class="iconfont icon-guanyu2"></i>
                    <span>关于</span>
                </a>
            </li>
            
            <li>
                <a id="search">
                    <i class="iconfont icon-sousuo1"></i>
                    <span>搜索</span>
                </a>
            </li>
            
        </ul>
    </div>
    
        <div id="toc" class="toc-article">
    
</div>
    
</div>


<div class="search-field" id="search-field">
    <div class="search-container">
        <div class="search-input">
            <span id="esc-search"> <i class="icon-fanhui iconfont"></i></span>
            <input id="search-input"/>
            <span id="begin-search">搜索</span>
        </div>
        <div class="search-result-container" id="search-result-container">

        </div>
    </div>
</div>

        <div class="index-about-mobile">
            <i> David Blog </i>
        </div>
    </div>
    
    <div class="index-middle">
        <!-- Main Content -->
        


<div class="post-container">
    <div class="post-title">
        【Shell脚本】贪吃蛇小游戏
    </div>

    <div class="post-meta">
        <span class="attr">发布于：<span>2015-08-08 11:11:11</span></span>
        
        <span class="attr">标签：/
        
        <a class="tag" href="/tags/#Shell" title="Shell">Shell</a>
        <span>/</span>
        
        
        </span>
        <span class="attr">访问：<span id="busuanzi_value_page_pv"></span>
</span>
</span>
    </div>
    <div class="post-content ">
        <figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#!/bin/bash</span></span><br><span class="line"></span><br><span class="line"><span class="comment">#方向为1上2右3下4左</span></span><br><span class="line">dir_u=1</span><br><span class="line">dir_r=2</span><br><span class="line">dir_d=3</span><br><span class="line">dir_l=4</span><br><span class="line">dir=<span class="variable">$dir_u</span></span><br><span class="line"><span class="comment">#m为面板宽度，n为面板高度</span></span><br><span class="line">m=60</span><br><span class="line">n=20</span><br><span class="line"><span class="comment">#蛇的起始位置</span></span><br><span class="line">a=$[m/2]</span><br><span class="line">b=$[n-1]</span><br><span class="line"><span class="comment">#食物产生的位置</span></span><br><span class="line">food_x=</span><br><span class="line">food_y=</span><br><span class="line"><span class="comment">#蛇的长度</span></span><br><span class="line">l=0</span><br><span class="line"><span class="comment">#蛇头数组下标</span></span><br><span class="line">sum=0</span><br><span class="line">len=0</span><br><span class="line"><span class="comment">#记录蛇头运行轨迹数组</span></span><br><span class="line">x=()</span><br><span class="line">y=()</span><br><span class="line"><span class="comment">#记录蛇撞击自己次数</span></span><br><span class="line">h=0</span><br><span class="line"></span><br><span class="line"><span class="comment">#主函数入口</span></span><br><span class="line"><span class="function"><span class="title">main</span></span>()&#123;	</span><br><span class="line">	run</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">#产生食物随机数</span></span><br><span class="line"><span class="function"><span class="title">ran_xy</span></span>()&#123;</span><br><span class="line">	food_y=$[RANDOM %(<span class="variable">$m</span>-2)+2]</span><br><span class="line">	food_x=$[RANDOM %(<span class="variable">$n</span>-2)+2]</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">#开始</span></span><br><span class="line"><span class="function"><span class="title">init</span></span>()&#123;</span><br><span class="line">	<span class="built_in">printf</span> <span class="string">"\033[1;1H"</span></span><br><span class="line">	<span class="built_in">printf</span> <span class="string">"\033[2J"</span></span><br><span class="line">	<span class="keyword">for</span> i <span class="keyword">in</span> $(seq <span class="variable">$n</span>)</span><br><span class="line">	<span class="keyword">do</span></span><br><span class="line">		<span class="keyword">for</span> j <span class="keyword">in</span> $(seq <span class="variable">$m</span>)</span><br><span class="line">		<span class="keyword">do</span></span><br><span class="line">		<span class="built_in">test</span> <span class="variable">$i</span> -eq 1 -o <span class="variable">$i</span> -eq <span class="variable">$n</span>  \</span><br><span class="line">			-o <span class="variable">$j</span> -eq 1 -o <span class="variable">$j</span> -eq <span class="variable">$m</span> \</span><br><span class="line">			 &amp;&amp;<span class="built_in">printf</span> <span class="string">"☒"</span> || <span class="built_in">printf</span> <span class="string">" "</span></span><br><span class="line">		<span class="keyword">done</span></span><br><span class="line">		<span class="built_in">printf</span> <span class="string">"\n"</span></span><br><span class="line">	<span class="keyword">done</span></span><br><span class="line">	<span class="built_in">printf</span> <span class="string">"    w：上    a：左    s：下     d：右     q：退出\n"</span></span><br><span class="line">	<span class="built_in">printf</span> <span class="string">"\033[%d;%dHo\n"</span> <span class="variable">$food_x</span> <span class="variable">$food_y</span></span><br><span class="line">	snake</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">#刷新地图</span></span><br><span class="line"><span class="function"><span class="title">run</span></span>()&#123;</span><br><span class="line"><span class="comment">#stty -echo取消跳动</span></span><br><span class="line">	stty -<span class="built_in">echo</span></span><br><span class="line">	<span class="built_in">printf</span> <span class="string">"\033[?25l"</span></span><br><span class="line">	ran_xy</span><br><span class="line">	<span class="keyword">while</span> <span class="literal">true</span>;<span class="keyword">do</span></span><br><span class="line">		init</span><br><span class="line">		move</span><br><span class="line">		eat</span><br><span class="line">		<span class="built_in">read</span> -t 0.3 -n 1 key</span><br><span class="line">		<span class="built_in">test</span> <span class="string">"<span class="variable">$key</span>"</span> = <span class="string">"q"</span> &amp;&amp; <span class="built_in">break</span></span><br><span class="line">		<span class="built_in">test</span> <span class="string">"<span class="variable">$key</span>"</span> = <span class="string">"w"</span> -a <span class="variable">$dir</span> != <span class="variable">$dir_d</span> &amp;&amp; dir=<span class="variable">$dir_u</span></span><br><span class="line">		<span class="built_in">test</span> <span class="string">"<span class="variable">$key</span>"</span> = <span class="string">"a"</span> -a <span class="variable">$dir</span> != <span class="variable">$dir_r</span> &amp;&amp; dir=<span class="variable">$dir_l</span></span><br><span class="line">		<span class="built_in">test</span> <span class="string">"<span class="variable">$key</span>"</span> = <span class="string">"s"</span> -a <span class="variable">$dir</span> != <span class="variable">$dir_u</span> &amp;&amp; dir=<span class="variable">$dir_d</span></span><br><span class="line">		<span class="built_in">test</span> <span class="string">"<span class="variable">$key</span>"</span> = <span class="string">"d"</span> -a <span class="variable">$dir</span> != <span class="variable">$dir_l</span> &amp;&amp; dir=<span class="variable">$dir_r</span></span><br><span class="line">		<span class="built_in">test</span> <span class="variable">$h</span> -eq 1 &amp;&amp; <span class="built_in">break</span></span><br><span class="line">	<span class="keyword">done</span>	</span><br><span class="line">	<span class="built_in">printf</span> <span class="string">"\033[?25h\033[%d;1H"</span> $[n+4]</span><br><span class="line">	stty <span class="built_in">echo</span></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">#移动操作</span></span><br><span class="line"><span class="function"><span class="title">move</span></span>()&#123;	</span><br><span class="line">	<span class="built_in">let</span> sum++</span><br><span class="line">	x[<span class="variable">$sum</span>]=<span class="variable">$a</span></span><br><span class="line">	y[<span class="variable">$sum</span>]=<span class="variable">$b</span></span><br><span class="line">	<span class="keyword">case</span> <span class="string">"<span class="variable">$dir</span>"</span> <span class="keyword">in</span></span><br><span class="line">	1)	<span class="built_in">let</span> b--</span><br><span class="line">	;;</span><br><span class="line">	2)	<span class="built_in">let</span> a++</span><br><span class="line">	;;</span><br><span class="line">	3)	<span class="built_in">let</span> b++</span><br><span class="line">	;;</span><br><span class="line">	4)	<span class="built_in">let</span> a--</span><br><span class="line">	<span class="keyword">esac</span></span><br><span class="line">	bump</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">#游戏结束</span></span><br><span class="line"><span class="function"><span class="title">game_kill</span></span>()&#123;</span><br><span class="line">	<span class="built_in">printf</span> <span class="string">"\033[7;20H"</span></span><br><span class="line">        <span class="built_in">printf</span> <span class="string">"\033[2J"</span></span><br><span class="line">	<span class="built_in">echo</span> -e <span class="string">"\033[31;60;1m  Game Over\033[39;49;0m"</span></span><br><span class="line">	<span class="built_in">printf</span> <span class="string">"\n\n"</span></span><br><span class="line">	<span class="built_in">echo</span> -e <span class="string">"\033[32;60;1m                 您的成绩为：$[l*10] 分\033[39;49;0m"</span></span><br><span class="line">	<span class="built_in">break</span></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">#画蛇</span></span><br><span class="line"><span class="function"><span class="title">snake</span></span>()&#123;</span><br><span class="line">	<span class="built_in">printf</span> <span class="string">"\033[%d;%dH@\n"</span> <span class="string">"<span class="variable">$b</span>"</span> <span class="string">"<span class="variable">$a</span>"</span></span><br><span class="line">        <span class="keyword">for</span> j <span class="keyword">in</span> $(seq 0 <span class="variable">$l</span>);<span class="keyword">do</span></span><br><span class="line">		len=$[sum-<span class="variable">$j</span>]</span><br><span class="line">		<span class="built_in">printf</span> <span class="string">"\033[%d;%dH0"</span> <span class="variable">$&#123;y[$len]&#125;</span> <span class="variable">$&#123;x[$len]&#125;</span></span><br><span class="line">	<span class="keyword">done</span></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">#蛇吃食物时</span></span><br><span class="line"><span class="function"><span class="title">eat</span></span>()&#123;</span><br><span class="line">	<span class="keyword">if</span> <span class="built_in">test</span> <span class="variable">$food_y</span> -eq <span class="variable">$a</span> &amp;&amp; <span class="built_in">test</span> <span class="variable">$food_x</span> -eq <span class="variable">$b</span>;<span class="keyword">then</span></span><br><span class="line">		ran_xy</span><br><span class="line">		<span class="built_in">let</span> l++</span><br><span class="line">	<span class="keyword">fi</span></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">#撞击时处理方法</span></span><br><span class="line"><span class="function"><span class="title">bump</span></span>()&#123;</span><br><span class="line">	<span class="built_in">test</span> <span class="variable">$a</span> -eq 1 -o <span class="variable">$b</span> -eq 1 -o <span class="variable">$a</span> -eq <span class="variable">$m</span> -o <span class="variable">$b</span> -eq <span class="variable">$n</span> &amp;&amp; game_kill</span><br><span class="line">	<span class="keyword">for</span> k <span class="keyword">in</span> $(seq 0 <span class="variable">$l</span>);	<span class="keyword">do</span></span><br><span class="line">		len=$[sum-<span class="variable">$k</span>]</span><br><span class="line">	<span class="keyword">if</span> <span class="built_in">test</span> <span class="variable">$&#123;y[$len]&#125;</span> -eq <span class="variable">$b</span> &amp;&amp; <span class="built_in">test</span> <span class="variable">$&#123;x[$len]&#125;</span> -eq <span class="variable">$a</span>;<span class="keyword">then</span></span><br><span class="line">		<span class="built_in">let</span> h++</span><br><span class="line">		game_kill</span><br><span class="line">	<span class="keyword">fi</span></span><br><span class="line">	<span class="keyword">done</span></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">main</span><br></pre></td></tr></table></figure>
        
            <div class="donate-container">
    <div class="donate-button">
        <button id="donate-button">赞赏</button>
    </div>
    <div class="donate-img-container hide" id="donate-img-container">
        <img id="donate-img" src="" data-src="/img/donate.jpg">
        <p> 感谢鼓励 </p>
    </div>
</div>
        
        <br />
        <div id="comment-container">
        </div>
        <div id="disqus_thread"></div>

        <div id="lv-container">
        </div>

    </div>
</div>

    </div>
</div>


<footer class="footer">
    <ul class="list-inline text-center">
        
        <li>
            <a target="_blank" href="https://twitter.com/qq986945193">
                            <span class="fa-stack fa-lg">
                                <i class="iconfont icon-twitter"></i>
                            </span>
            </a>
        </li>
        
        
        <li>
            <a target="_blank" href="https://www.zhihu.com/people/mcxiaobing">
                            <span class="fa-stack fa-lg">
                                 <i class="iconfont icon-zhihu"></i>
                            </span>
            </a>
        </li>
        

        
        <li>
            <a target="_blank" href="http://weibo.com/mcxiaobing">
                            <span class="fa-stack fa-lg">
                                  <i class="iconfont icon-weibo"></i>
                            </span>
            </a>
        </li>
        

        
        <li>
            <a target="_blank" href="https://www.facebook.com/qq986945193">
                            <span class="fa-stack fa-lg">
                                <i class="iconfont icon-facebook"></i>
                            </span>
            </a>
        </li>
        

        
        <li>
            <a target="_blank"  href="https://github.com/qq986945193">
                            <span class="fa-stack fa-lg">
                                <i class="iconfont icon-github"></i>
                            </span>
            </a>
        </li>
        

        

    </ul>
    
    <p>
        <span>/</span>
        
        <span><a href="https://segmentfault.com/u/mcxiaobing">思否</a></span>
        <span>/</span>
        
        <span><a href="https://juejin.im/user/57edcdb2a0bb9f0058ed8ed9">掘金</a></span>
        <span>/</span>
        
        <span><a href="https://www.jianshu.com/u/4e2015c5a972">简书</a></span>
        <span>/</span>
        
        <span><a href="https://gitee.com/mcxiaobing">码云</a></span>
        <span>/</span>
        
    </p>
    
    <p>
        <span id="busuanzi_container_site_pv">
            <span id="busuanzi_value_site_pv"></span>PV
        </span>
        <span id="busuanzi_container_site_uv">
            <span id="busuanzi_value_site_uv"></span>UV
        </span>
        Created By <a href="https://hexo.io/">Hexo</a>  Theme <a href="https://github.com/aircloud/hexo-theme-aircloud">AirCloud</a></p>
</footer>




</body>

<script>
    // We expose some of the variables needed by the front end
    window.hexo_search_path = "search.json"
    window.hexo_root = "/"
    window.isPost = true
</script>
<script src="https://cdn.bootcss.com/jquery/3.3.1/jquery.min.js"></script>
<script src="/js/index.js"></script>
<script async src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>




</html>
